<template>
	<view>
		<view class="p10">
			<u--input prefixIcon="search" placeholder="请输入商品名称" border="none" shape="circle"
				:customStyle="{background:'#fff',height:'60rpx',paddingLeft:'20rpx'}" v-model="formInline.name"
				@input="toSearch">
			</u--input>
		</view>
		<view class="card c3 mb10 cshow" v-for="item in tableData" :key="item.id">
			<view class="content">
				<view style="display: flex;justify-content: space-between;align-items: center;">
					<view class="content-text" style="font-size: 34rpx;font-weight: 600;">
						#{{ item.id }}
					</view>
					<view class="content-text" style="display: flex;align-items: center;">
						<u-tag v-if="item.frame_state" type="error" text="下架" size="mini" plain></u-tag>
						<u-tag v-if="!item.frame_state" type="success" text="上架" size="mini" plain></u-tag>
					</view>
				</view>
				<u-divider></u-divider>
				<image style="width: 100%;height: 380rpx;border-radius: 15rpx;" :src="item.image_list[0]" lazy-load>
				</image>
				<view class="content-text" style="font-size: 32rpx;font-weight: 600;">{{item.product_name}}</view>
				<view class="content-text" v-if="item.dy_product_id != '' && item.dy_product_id != null">
					商品ID：{{item.dy_product_id}}</view>
				<view class="content-text">
					售卖时间：{{item.sold_start_time}} - {{item.sold_end_time}}</view>
				<u-divider></u-divider>
				<view class="footer">
					<view class="footer_item" @click="editShang(item)" v-if="item.frame_state && adminInfo.store_id!=0">
						上架
					</view>
					<view class="footer_item" @click="editXia(item)" v-if="!item.frame_state && adminInfo.store_id!=0">
						下架
					</view>
				</view>
			</view>
		</view>
		<view v-if="tableData.length==0" class="np f-c-c">
			<view class="iconfont icon-queshengtu cb" style="font-size: 90rpx;"></view>
			<view class="f12 cb mt20">暂无商品</view>
		</view>
	</view>
</template>

<script>
	import site from '@/custom/siteroot.js';
	export default {
		data() {
			return {
				formInline: {
					limit: 10,
					name: "",
					page: 1,
					product_id: "",
				},
				tableData: [],
				hasLoadedData: false,
				adminInfo: uni.getStorageSync("adminInfo"),
			}
		},
		methods: {
			toSearch() {
				this.hasLoadedData = false;
				this.getlists()
			},
			getlists() {
				uni.showLoading()
				this.beg.request({
					url: '/admin/product.Product/getStoreProductList',
					method: 'post',
					data: this.formInline
				}).then(res => {
					if (res.code == 1) {
						res.data.data.forEach(v => {
							if (v.image_list != '') {
								v.image_list = JSON.parse(v.image_list);
								v.image_list = v.image_list.map(i => {
									return site.siteimgroot + i;
								});
							}
						});
						let newData = res.data.data
						if (!this.hasLoadedData) {
							this.tableData = newData;
							this.hasLoadedData = true;
						} else {
							this.tableData = this.tableData.concat(newData);
						}
					} else {
						uni.$u.toast(res.msg)
					}
					uni.hideLoading()
				})
			},
			//上架
			editShang(val) {
				//查询数据
				this.beg.request({
						url: '/index.php/admin/product.Product/includeProduct',
						method: 'post',
						data: {
							product_id: val.id,
						}
					})
					.then((res) => {
						if (res.code == 1) {
							uni.$u.toast(res.msg)
							setTimeout(res => {
								this.hasLoadedData = false;
								this.formInline.page = 1
								this.getlists();
							}, 800)

						} else {
							uni.$u.toast(res.msg)
						}
					})
					.catch(() => {})
			},
			//下架
			editXia(val) {
				//查询数据
				this.beg.request({
						url: '/index.php/admin/product.Product/excludeProduct',
						method: 'post',
						data: {
							product_id: val.id,
						}
					})
					.then((res) => {
						if (res.code == 1) {
							uni.$u.toast(res.msg)
							setTimeout(res => {
								this.hasLoadedData = false;
								this.formInline.page = 1
								this.getlists();
							}, 800)
						} else {
							uni.$u.toast(res.msg)
						}
					})
					.catch(() => {})
			},
			showTips() {
				uni.getStorage({
					key: 'pageVisitCount',
					complete: (res) => {
						let count = res.data || 0;
						if (count < 3) {
							uni.showModal({
								title: '温馨提示',
								content: '该操作为连锁门店中的单个门店，可以上下架自己的产品，操作商品是否上下架(例如临时卖完了，下架即可)，下架该产品用户就无法下单这个产品，上架即可继续售卖！',
								showCancel: false,
								success: () => {
									uni.setStorage({
										key: 'pageVisitCount',
										data: count + 1
									});
								}
							});
						} else {
							console.log("已提示过三次，不再显示");
						}
					},
				});
			}
		},
		onLoad() {
			this.getlists()
			this.showTips();
		},
		onReachBottom() {
			this.formInline.page++
			this.getlists();
		}
	}
</script>
<style lang="scss" scoped>
	.content-text {
		margin-top: 10rpx;
		color: #000;
		font-size: 28rpx;
		font-weight: 500;
	}

	.setting {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		align-items: center;
	}

	.card {
		width: 94%;
		margin: 0 auto;
		margin-top: 38rpx;
	}

	.navbar2 {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 100rpx;
		background: #fff;
		box-shadow: 0 0rpx 8rpx 4rpx rgba(0, 0, 0, 0.05);
	}

	.footer {
		display: flex;
		justify-content: flex-start;
		align-items: center;

		.footer_item {
			width: 125rpx;
			height: 55rpx;
			border-radius: 8rpx;
			background-color: #fdda34;
			color: #000;
			font-weight: 600;
			display: flex;
			justify-content: center;
			align-items: center;
		}

		.footer_item:not(:first-child) {
			margin-left: 30rpx;
		}
	}
</style>